package com.newly.center.sys.service.device.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newly.center.sys.service.device.api.IDeviceService;
import com.newly.common.base.entity.device.dto.DeviceDto;
import com.newly.common.base.entity.device.po.DevicePo;
import com.newly.common.base.entity.device.vo.DeviceMonitorVo;
import com.newly.common.core.utils.PageUtil;
import com.newly.common.mapper.business.SiteMapper;
import com.newly.common.mapper.sys.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/11/25 9:49
 */
@Service
public class DeviceService implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private SiteMapper siteMapper;

    @Override
    public List<String> getNames() {

        List<DevicePo> devicePos = this.deviceMapper.selectList(new QueryWrapper<>());
        List<String> deviceNames = devicePos.stream().map(DevicePo::getName).collect(Collectors.toList());
        return deviceNames;
    }

    @Override
    public PageUtil monitor(DeviceDto deviceDto) {

        Page<DeviceMonitorVo> page = new Page<>(deviceDto.getPageNum(), deviceDto.getPageSize());
        page = this.deviceMapper.getSitePage(page, deviceDto);
        if (ObjectUtil.isEmpty(page.getRecords()))
            return new PageUtil(page);
        List<DeviceMonitorVo> deviceMonitorVos = page.getRecords();
        List<String> siteIds = deviceMonitorVos.stream().map(DeviceMonitorVo::getSiteId).collect(Collectors.toList());
        List<DevicePo> devicePos = this.deviceMapper.selectList(new LambdaQueryWrapper<DevicePo>().in(DevicePo::getSiteId, siteIds).eq(DevicePo::getIsDeleted, 0));
        //设置站点名称
        devicePos = devicePos.stream().map(p -> deviceMonitorVos.stream().filter(m ->
                Objects.equals(p.getSiteId().toString(), m.getSiteId()))
                .findFirst().map(m -> {
                    p.setSiteName(m.getSiteName());
                    p.setSiteAgent(m.getSiteAgent());
                    p.setPhone(m.getPhone());
                    return p;
                }).orElse(null))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        //按站点名称分组
        Map<String, List<DevicePo>> map = devicePos.stream().collect(Collectors.groupingBy(DevicePo::getSiteName));
        for (Map.Entry<String, List<DevicePo>> entry : map.entrySet()) {
            for (DeviceMonitorVo deviceMonitorVo : deviceMonitorVos) {
                if (entry.getKey().equals(deviceMonitorVo.getSiteName())) {
                    devicePos = entry.getValue().stream().filter(p -> p.getType() == 3 && Integer.valueOf(p.getDeviceStatus()) == 0).collect(Collectors.toList());
                    deviceMonitorVo.setSnapStatus(devicePos.size() > 0 ? 1 : 0);
                    devicePos = entry.getValue().stream().filter(p -> p.getType() == 1 && Integer.valueOf(p.getDeviceStatus()) == 0).collect(Collectors.toList());
                    deviceMonitorVo.setWeighingStatus(devicePos.size() > 0 ? 1 : 0);
                    deviceMonitorVo.setSiteStatus(deviceMonitorVo.getSnapStatus() == 1 || deviceMonitorVo.getWeighingStatus() == 1 ? 1 : 0);
                }
            }
        }
        page.setRecords(deviceMonitorVos);
        return new PageUtil(page);
    }
}
